package com.pc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pc.enrty.Order;
import com.pc.pojo.OrderAndOrderitems;
import com.pc.pojo.OrderitemsAndOrderAndFood;
import com.pc.enrty.Order;
import com.pc.enrty.Orderitems;
import com.pc.service.FoodService;
import com.pc.service.OrderItemsService;
import com.pc.service.OrderService;
import com.pc.util.ResultObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/orderitem/")
public class OrderitemsController {
    @Autowired
    private OrderItemsService orderItemsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private FoodService foodService;
    @Autowired
    private ResultObject result;
    @RequestMapping("addOrderItems")
    public boolean addOrderItems(Orderitems orderitems){
        ArrayList<Order> order = orderService.findOrder(new Order());
        int last = order.size()-1;
        orderitems.setOid(order.get(last).getId());//得到   oid
        System.out.println(orderitems);
        System.out.println("===================");
        boolean boo = orderItemsService.addOrderItems(orderitems);
        return boo;
    }
    @Autowired
    private OrderAndOrderitems orderAndOrderitems;

    @RequestMapping("showOrderItemsList")
    public ResultObject showOrderItemsList(Integer userid,Integer pageindex){
        result.setStatus(pageindex);
        if(pageindex==null){
            PageHelper.startPage(1,2);
        }else {
            PageHelper.startPage(pageindex,2);
        }
        ArrayList<Order> order = orderItemsService.findOrder(userid);
        for (Order o :order){
            if ("A".equals(o.getStatus())||o.getStatus().equals("已支付")){
                o.setStatus("已支付");
            }else {
                o.setStatus("未支付");
            }
        }
        PageInfo<Order> orderPageInfo = new PageInfo<>(order);
        ArrayList<OrderitemsAndOrderAndFood> orderitemsAndOrderAndFoods = orderItemsService.selectByUserid(userid);
        for (OrderitemsAndOrderAndFood o :orderitemsAndOrderAndFoods){
            if ("A".equals(o.getStatus())||o.getStatus().equals("已支付")){
                o.setStatus("已支付");
            }else {
                o.setStatus("未支付");
            }
        }
        orderAndOrderitems.setArrayList(orderPageInfo);
        orderAndOrderitems.setArrayLists(orderitemsAndOrderAndFoods);
        result.setResult(orderAndOrderitems);
        return result;
    }

    @RequestMapping("payOrderItems")
    public ResultObject payOrderItems(String ordernumber){
        System.out.println(ordernumber);
        int i = orderItemsService.updateOrderItems(ordernumber);
        if(i==1){
            result.setStatus(400);
            result.setResult(i==1?true:false);
        }else {
            result.setStatus(404);
            result.setResult(i==1?true:false);
        }
        return result;
    }




}
